//
//  GDNewsChannelView.m
//  爱新奇－味道
//
//  Created by 热带雨林 on 17/2/17.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDNewsChannelView.h"
#import "Masonry.h"
#import "GDNewsLabel.h"
#import "GDNewsChannelModel.h"


@interface GDNewsChannelView()
@property(nonatomic, weak)UIScrollView *scrollView;

@end

@implementation GDNewsChannelView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}
- (void)setupUI{

    self.backgroundColor = [UIColor whiteColor];
    //添加一个UISrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    self.scrollView = scrollView;
    [self addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
}
//开始给控件赋值
- (void)setChannelModelList:(NSArray<GDNewsChannelModel *> *)ChannelModelList{
    _ChannelModelList = ChannelModelList;
    
    CGFloat margin = 5;
    CGFloat x = 5;
    CGFloat y = 0;
    CGFloat width = 0;
    CGFloat height = 30;
    
    for(NSInteger i =0;i<ChannelModelList.count; i++){
        GDNewsChannelModel *channlModel = ChannelModelList[i];
        
        GDNewsLabel *label = [GDNewsLabel addLabelWithTitle:channlModel.tname];
        width = label.frame.size.width;
        [label setFrame:CGRectMake(x, y, width, height)];
        //挨个设置间距
        x += width + margin;
        [self.scrollView addSubview:label];
        //使用tag 记录一下是哪一个label
        label.tag = i;
        //label的点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taplabel:)];
        //将点击手势添加到label中
        [label addGestureRecognizer:tap];
    }
    //设置滚动范围
    [self.scrollView setContentSize:CGSizeMake(x, height)];
    
}
- (void)taplabel:(UITapGestureRecognizer *)sender{
    
    GDNewsLabel *label = (GDNewsLabel *)sender.view;
    NSInteger currentI = label.tag;
    
    self.currentIndexP =currentI;
    //监听值的改变
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

//- (void)channelLabelWithScale:(float)scale indexP:(NSInteger)indexP{
//
//    //拿到相对应的label
//    GDNewsLabel *label = self.scrollView.subviews[0];
//    label.progress = scale;
//    
//}
- (void)channelLabelWithScale:(float)scale indexP:(NSInteger)indexP{
    GDNewsLabel *label = self.scrollView.subviews[0];
    label.progress = scale;
}
@end
